// for range自动检测关闭并阻塞
package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println("start goroutine...")
	var output = make(chan int)
	go producer(output)

	// 从通道接收所有结果
	for v := range output {
		fmt.Println("Result:", v)
	}
}

// 异步处理
func producer(chal chan<- int) {
	fmt.Println("running goroutine start...")
	for i := 0; i < 3; i++ {
		time.Sleep(time.Second)
		chal <- i
	}
	fmt.Println("running goroutine end...")
	close(chal)
}
